package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;

public class StartMeetingListPanel extends ContentPanel {
	private ListStore<Meeting> startStore = new ListStore<Meeting>();
	private ListStore<Meeting> cancelStore = new ListStore<Meeting>();

	private GridCellRenderer<Meeting> date;
	private ColumnModel cm;

	public StartMeetingListPanel() {
		setHeading("发起会议列表");

		createStartPanel();
		createCancelPanel();
	}

	//已取消的会议
	private void createCancelPanel() {
		ContentPanel cp = new ContentPanel();
		cp.setHeading("已取消的会议");
		cp.setStyleAttribute("padding", "5px");
		cp.setHeight(300);
		cp.setFrame(true);

		final DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");

		date = new GridCellRenderer<Meeting>() {
			@Override
			public Object render(Meeting model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<Meeting> store, Grid<Meeting> grid) {
				Integer cancelTAG = (Integer) model.getCancelTAG();
				Date val = (Date) model.get(property);
				if (cancelTAG != null && cancelTAG == 1) {
					return "<span style='color: red;text-decoration: line-through;'>"
							+ dtf.format(val)
							+ "</span>&nbsp;&nbsp;<span style='color: red;font-weight: bold;'>已取消</span>";
				} else {
					return "<span>" + dtf.format(val) + "</span>";
				}
			}
		};

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("originatorName");
		column.setHeader("发起人");
		column.setWidth(100);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("chairName");
		column.setHeader("主席");
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("主题");
		column.setWidth(400);
		configs.add(column);

		column = new ColumnConfig("startDate", "会议时间", 150);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(date);
		configs.add(column);

		cm = new ColumnModel(configs);

		Grid<Meeting> grid = new Grid<Meeting>(cancelStore, cm);
		grid.setAutoExpandColumn("subject");
		grid.setHeight(300);
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		cp.add(grid);

		grid.setStateful(true);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<Meeting>>() {
					public void handleEvent(SelectionChangedEvent<Meeting> be) {
						if (be.getSelection().size() > 0) {
							String id = be.getSelection().get(0).getId();
							Dispatcher.forwardEvent(AppEvents.ToMeetingContent,
									id);
						}
					}
				});

		add(cp);
	}

	//由我发起的会议
	private void createStartPanel() {
		ContentPanel cp = new ContentPanel();
		cp.setHeading("由我发起的会议");
		cp.setStyleAttribute("padding", "5px");
		cp.setHeight(300);
		cp.setFrame(true);

		final DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");

		date = new GridCellRenderer<Meeting>() {
			@Override
			public Object render(Meeting model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<Meeting> store, Grid<Meeting> grid) {
				Integer cancelTAG = (Integer) model.getCancelTAG();
				Date val = (Date) model.get(property);
				if (cancelTAG != null && cancelTAG == 1) {
					return "<span style='color: red;text-decoration: line-through;'>"
							+ dtf.format(val)
							+ "</span>&nbsp;&nbsp;<span style='color: red;font-weight: bold;'>已取消</span>";
				} else {
					return "<span>" + dtf.format(val) + "</span>";
				}
			}
		};

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("originatorName");
		column.setHeader("发起人");
		column.setWidth(100);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("chairName");
		column.setHeader("主席");
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("主题");
		column.setWidth(400);
		configs.add(column);

		column = new ColumnConfig("startDate", "会议时间", 150);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(date);
		configs.add(column);

		cm = new ColumnModel(configs);

		Grid<Meeting> grid = new Grid<Meeting>(startStore, cm);
		grid.setAutoExpandColumn("subject");
		grid.setHeight(300);
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		cp.add(grid);

		grid.setStateful(true);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<Meeting>>() {
					public void handleEvent(SelectionChangedEvent<Meeting> be) {
						if (be.getSelection().size() > 0) {
							String id = be.getSelection().get(0).getId();
							Dispatcher.forwardEvent(AppEvents.ToMeetingContent,
									id);
						}
					}
				});

		add(cp);
	}

	public ListStore<Meeting> getStartStore() {
		return startStore;
	}

	public ListStore<Meeting> getCancelStore() {
		return cancelStore;
	}
}
